package com.lanou.musicproject.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.SingerInfoActitity;
import com.lanou.musicproject.adapter.RightAllGridAdapter;
import com.lanou.musicproject.bean.MusicResult;
import com.lanou.musicproject.bean.MusicRoot;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/22.
 * 音乐馆右侧
 */
public class RightAllFragment extends Fragment implements AdapterView.OnItemClickListener {
    private GridView mGridView;
    private String url;
    private TextView topName;
    private VolleySingleton volleySingleton;
    private ArrayList<MusicResult> datas;
    private RightAllGridAdapter allGridAdapter;

    public RightAllFragment(){}

    @SuppressLint("ValidFragment")
    public RightAllFragment(String url) {
        this.url = url;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View allView = inflater.inflate(R.layout.fragment_allright, null);
        initView(allView);
        return allView;
    }
    /**
     * 加载布局
     */
    private void initView(View v) {
        volleySingleton = VolleySingleton.getInstance(getActivity());

        mGridView = (GridView) v.findViewById(R.id.allfragment_gridview);
        getData();
        mGridView.setOnItemClickListener(this);
    }

    /**
     * 解析数据方法
     */
    private void getData() {
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String string = response.toString();
                Gson gson = new Gson();
                MusicRoot root = gson.fromJson(string, MusicRoot.class);
                datas = root.getData();
                allGridAdapter = new RightAllGridAdapter(datas, getActivity());
                mGridView.setAdapter(allGridAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }

    /**
     * grid监听
     * @param parent
     * @param view
     * @param position
     * @param id
     * 跳转到歌曲信息页面
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MusicResult result = (MusicResult) parent.getItemAtPosition(position);
        Intent toNext = new Intent(getActivity(), SingerInfoActitity.class);
        toNext.putExtra("result", result);
        getActivity().startActivity(toNext);
    }

}
